package com.aca.market.service.impl;

import com.aca.market.entity.User;
import com.aca.market.mapper.UserMapper;
import com.aca.market.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author aca
 * @since 2020-11-13
 */
@Transactional
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;

    public synchronized Session nowSession(){
        return SecurityUtils.getSubject().getSession();
    }

    @Override
    //确认是否有登录用户 有则返回
    public synchronized User nowUser(){
        try {
//            Object tmp = SecurityUtils.getSubject().getSession().getAttribute("user");
            Subject subject = SecurityUtils.getSubject();
            Object tmp = subject.getPrincipal();
            log.info("=====>nowUser: {}",tmp);
            if (tmp != null){
                return (User) tmp;
            }
        } catch (InvalidSessionException e) {
            e.printStackTrace();
            log.info("===============无Session");
        }
        return null;
    }


}
